package org.example.wx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.common.utils.ResponseUtil;
import org.example.db.entity.Goods;
import org.example.wx.dto.GoodsDetailDto;
import org.example.wx.dto.GoodsGeneralDto;
import org.example.wx.dto.GoodsIdGoodsSpecIdDto;
import org.example.wx.service.WxFootPrintService;
import org.example.wx.service.WxGoodsService;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

@RestController
@Api(tags = "商品")
@RequestMapping("/wx/goods")
public class WxGoodsController {

    WxGoodsService wxGoodsService;
    WxFootPrintService wxFootPrintService;

    public WxGoodsController(WxGoodsService wxGoodsService, WxFootPrintService wxFootPrintService) {
        this.wxGoodsService = wxGoodsService;
        this.wxFootPrintService = wxFootPrintService;
    }

    @GetMapping("/category/list")
    @ApiOperation("商品列表")
    public Object list(
            @RequestParam Long categoryId,
            @RequestParam Integer currentPage,
            @RequestParam Integer pageSize,
            @RequestParam(defaultValue = "sort_order") String sort,
            @RequestParam(defaultValue = "desc") String order
    ){


        IPage<Goods> pageGoodsList = wxGoodsService.getPageGoodsByCategoryId(new Page<Goods>(currentPage, pageSize), categoryId, sort, order);


        return ResponseUtil.ok(pageGoodsList.getRecords());
    }

    @GetMapping("/detail")
    @ApiOperation("商品详细信息")
    public Object detail(@RequestParam Long id){

        GoodsDetailDto goodsDetailDto = wxGoodsService.getGoodsDetail(id);
        if(goodsDetailDto != null){
            wxFootPrintService.add(id);
        }
        return ResponseUtil.ok(goodsDetailDto);
    }

    @PostMapping("/general")
    @ApiOperation("商品简略信息")
    public Object general(@RequestBody List<GoodsIdGoodsSpecIdDto> generalList){

        LinkedList<GoodsGeneralDto> goodsGeneralDtoList = new LinkedList<>();
        for(GoodsIdGoodsSpecIdDto general: generalList){
            GoodsGeneralDto goodsGeneral = wxGoodsService.getGoodsGeneral(general.getGoodsId(), general.getGoodsSpecId());
            goodsGeneral.setNumber(general.getNumber());
            goodsGeneralDtoList.add(goodsGeneral);
        }

        return ResponseUtil.ok(goodsGeneralDtoList);
    }
}
